//
//  HomeHeader.m
//  敢唆嘛!
//
//  Created by 教主 on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "HomeHeader.h"
#import "Device.h"

@interface HomeHeader () <UIScrollViewDelegate>
@property(nonatomic, strong)UIScrollView *homeHeaderScrollView;     // 创建首页区头部滚动视图
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)UIPageControl *page;
@end

@implementation HomeHeader
{
    NSInteger number;
    BOOL isBack;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        CGFloat x = 10;
        // 创建顶部字
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, x, KSWidth - x * 2, KSWidth / 22)];
        titleLabel.text = @"在故事里相遇";
        titleLabel.alpha = 0.3;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        [self.contentView addSubview:titleLabel];
        
        // 创建首页区头部滚动视图
        self.homeHeaderScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, titleLabel.frame.size.height + x * 2, KSWidth, KSWidth / 2.2)];
        self.homeHeaderScrollView.contentSize = CGSizeMake(KSWidth * 4, KSWidth / 2.2);
        self.homeHeaderScrollView.contentOffset = CGPointMake(KSWidth * 0, 0);
        self.homeHeaderScrollView.pagingEnabled = YES;
        self.homeHeaderScrollView.bounces = NO;
        self.homeHeaderScrollView.delegate = self;
        // 隐藏滑动条
        self.homeHeaderScrollView.showsHorizontalScrollIndicator = NO;
        self.homeHeaderScrollView.showsVerticalScrollIndicator = NO;
        
        // 创建四张广告图
        self.homeHeaderImage1 = [[UIImageView alloc] initWithFrame:CGRectMake(KSWidth * 0, 0, KSWidth, KSWidth / 2)];
        self.homeHeaderImage1.userInteractionEnabled = YES;
        [self.homeHeaderScrollView addSubview:_homeHeaderImage1];
        self.homeHeaderImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(KSWidth * 1, 0, KSWidth, KSWidth / 2)];
        self.homeHeaderImage2.userInteractionEnabled = YES;
        [self.homeHeaderScrollView addSubview:_homeHeaderImage2];
        self.homeHeaderImage3 = [[UIImageView alloc] initWithFrame:CGRectMake(KSWidth * 2, 0, KSWidth, KSWidth / 2)];
        self.homeHeaderImage3.userInteractionEnabled = YES;
        [self.homeHeaderScrollView addSubview:_homeHeaderImage3];
        self.homeHeaderImage4 = [[UIImageView alloc] initWithFrame:CGRectMake(KSWidth * 3, 0, KSWidth, KSWidth / 2)];
        self.homeHeaderImage4.userInteractionEnabled = YES;
        [self.homeHeaderScrollView addSubview:_homeHeaderImage4];
        [self.contentView addSubview:_homeHeaderScrollView];
        
        // 创建pageControl
        CGFloat titleLabelHeight = titleLabel.frame.size.height;
        CGFloat homeHeaderScrollViewHeight = self.homeHeaderScrollView.frame.size.height;
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, titleLabelHeight +homeHeaderScrollViewHeight, 5, 5)];
        self.page.numberOfPages = 4;
        self.page.currentPage = 0;
        [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_page];
        
        // 创建定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(setOffset) userInfo:nil repeats:YES];
        number = 0;
    }
    return self;
}

// 自动滑动图片
- (void)setOffset
{
    if (number == 3)
    {
        isBack = YES;
    }
    else if (number == 0)
    {
        isBack = NO;
    }
    
    if (!isBack)
    {
        number++;
        [self.homeHeaderScrollView setContentOffset:CGPointMake(KSWidth * number, 0) animated:YES];
//        if (number == 3)
//        {
//            self.page.currentPage = number+1;
//        }
        
//        {
            self.page.currentPage = number;
//        }
    }
    else
    {
        number--;
        [self.homeHeaderScrollView setContentOffset:CGPointMake(KSWidth * number, 0) animated:YES];
        self.page.currentPage = number;
    }
    

}

// scrollView代理方法 结束加速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *page = self.contentView.subviews[2];
    page.currentPage = self.homeHeaderScrollView.contentOffset.x / KSWidth;
}

// page方法
- (void)pageAction:(UIPageControl *)page
{
    // 点击小圆点的时候 改变scrollView的偏移量
    [self.homeHeaderScrollView setContentOffset:CGPointMake(page.currentPage * KSWidth, 0) animated:YES];
}

// 传值
- (void)setModel:(HomeHeaderModel *)model
{
    // 主页头部广告
    [self.homeHeaderImage1 sd_setImageWithURL:[NSURL URLWithString:model.homeHeaderImage1]];
    [self.homeHeaderImage2 sd_setImageWithURL:[NSURL URLWithString:model.homeHeaderImage2]];
    [self.homeHeaderImage3 sd_setImageWithURL:[NSURL URLWithString:model.homeHeaderImage3]];
    [self.homeHeaderImage4 sd_setImageWithURL:[NSURL URLWithString:model.homeHeaderImage4]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
